<?php
/*
*	Kleophatra - Open Source Content Management System
*	------------------------------------------------------------------------
*	Copyright (C) 2011, Sergio Milardovich <smilardovich@yahoo.com.ar>
*	This program is Free Software.
*	permalinks.lib.php: Automatically pretty URLs generator based on Agilis (https://github.com/milardovich/Snippets/tree/master/PHP/Agilis)
*
*	@package	Kleophatra
*	@subpackage	/lib
*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
*	@author		Sergio Milardovich <smilardovich@yahoo.com.ar>
*	@link		http://www.kleophatra.org
*/
class Permalinks {
	function GetRoute(){
		$base = $_SERVER['REQUEST_URI'];

		/* 
		 * This piece of code was written by Xiam, you can take a look at the original version here: http://git.hckr.org/web/core.git
		 */
		if (preg_match('/(.*)\?(.*)/', $base, $m) && isset($m[2])) {
			$base_script = $m[1];
			$args = explode('&', $m[2]);
			foreach ($args as $arg) {
				list($var, $val) = explode('=', $arg);
				if($var !== 'pretty'){
					$orig_args[$var] = $val;
				}
			}
		}
		if(isset($base_script)){
			$newurl = array();
			$values = array();
			foreach($orig_args as $var => $val){
				array_push($newurl, $val);
				array_push($values, $var.'->'.$val);
			}
			$this->values = implode("/",$values);
			$this->route = implode("/",$newurl);
		}
	}
	function Redirect(){
		if(isset($this->route)){
			$_SESSION['modules:values'] = $this->values;
			header("Location: ".$this->route,TRUE,301);
		}
	}
}
?>
